<template>
  <el-drawer v-model="drawerVisible" :z-index="1000" :destroy-on-close="true" size="500px" :title="`${drawerProps.title}`">
    <el-form
      ref="AuthFormRef"
      label-width="130px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="申请人">
        <el-input v-model="drawerProps.row!.empid" placeholder="请填写申请人" readonly></el-input>
      </el-form-item>

      <el-form-item label="门禁点描述">
        <el-input v-model="drawerProps.row!.doorname" placeholder="请填写申请点描述" readonly></el-input>
      </el-form-item>
      <el-form-item label="申请理由">
        <el-input v-model="drawerProps.row!.reason" placeholder="请填写申请理由" readonly></el-input>
      </el-form-item>

      <el-form-item label="审批备注" prop="remark">
        <el-input v-model="drawerProps.row!.remark" placeholder="请填写审批备注" clearable></el-input>
      </el-form-item>
      <el-form-item label="审批结果" prop="approvalresult">
        <el-select v-model="drawerProps.row!.approvalresult" placeholder="请选择审批结果" clearable>
          <el-option label="通过" :value="1" />
          <el-option label="拒绝" :value="2" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="_colse">取消</el-button>
      <el-button type="primary" :loading="loading" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="HotelDrawer">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";

const loading = ref(false);

const rules = reactive({
  remark: [{ required: true, trigger: "blur", message: "请填写审批备注" }],
  approvalresult: [{ required: true, message: "请选择审批结果" }]
});

interface DrawerProps {
  title: string;
  row: any;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
  resetTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  title: "",
  row: {}
});
// 接收父组件传过来的参数
const acceptParams = async (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const AuthFormRef = ref<FormInstance>();
/**
 * 关闭弹窗
 */
const _colse = () => {
  drawerVisible.value = false;
  AuthFormRef.value && AuthFormRef.value.resetFields();
};
/**
 * 提交信息
 */
const handleSubmit = () => {
  AuthFormRef.value!.validate(async valid => {
    if (!valid) return;
    loading.value = true;
    try {
      await drawerProps.value.api!({
        ...drawerProps.value.row
      });
      ElMessage.success({ message: `${drawerProps.value.title}成功！` });
      drawerProps.value.getTableList!();
      loading.value = false;
      drawerVisible.value = false;
    } catch (error) {
      loading.value = false;
      console.log(error);
    }
  });
};
defineExpose({
  acceptParams
});
</script>
